import { Component, OnInit, Input } from '@angular/core';
import { AppGlobalService } from '../../shared/service/app-global.service';
import { PageModalService } from '../../shared/service/page-modal-service';
import { HttpService } from '../../shared/service/http.service';
import { AlertController } from '@ionic/angular';

@Component({
  selector: 'app-publish-title',
  templateUrl: './publish-title.component.html',
  styleUrls: ['./publish-title.component.scss'],
})
export class PublishTitleComponent implements OnInit {
  @Input() tagData: any; // 标签数据
  listdata: any;
  isLogin = false;
  headTitle: any;
  headTitleCssStyle: any;
  style6MainBoxCssStyle: any;
  middleLineCssStyle: any;
  needSplitIntoLeftOrRight: any;
  leftListData = [];
  rightListData = [];
  constructor(
    private appGlobalService: AppGlobalService,
    private pageModalService: PageModalService,
    private http: HttpService,
    private alertController: AlertController
  ) { }

  ngOnInit() {
    this.isLogin = localStorage.getItem("loginStatus") ? true : false;
    this.tagData.data.data.data.forEach((element, index) => {
      if (index === 0 && element && element.headTitle !== undefined) {
        this.headTitle = element.headTitle;
        if (element.headTitleCssStyle) {
          this.headTitleCssStyle = element.headTitleCssStyle;
        }
        if (element.style6MainBoxCssStyle) {
          this.style6MainBoxCssStyle = element.style6MainBoxCssStyle;
        }
        if (element.middleLineCssStyle) {
          this.middleLineCssStyle = element.middleLineCssStyle;
        }

        if (element.needSplitIntoLeftOrRight !== undefined) {
          this.needSplitIntoLeftOrRight = true;
        }
      }

      if (this.needSplitIntoLeftOrRight) {
        if ((index + 1) % 4 === 1 || (index + 1) % 4 === 2) {
          this.leftListData.push(element);
        } else if ((index + 1) % 4 === 3 || (index + 1) % 4 === 0) {
          this.rightListData.push(element);
        }
      }
      if (element.cssStyle && element.cssStyle !== "") {
        element.cssStr = {};
        const cssStyles = element.cssStyle.split(";");
        cssStyles.map(v => {
          const cssStrs = v.split(":");
          element.cssStr[cssStrs[0]] = cssStrs[1];
        });
      }
    });
    console.log(new Date().toLocaleString() + '.' + new Date().getMilliseconds() + '  	 leftListData  ', this.leftListData);
    console.log(new Date().toLocaleString() + '.' + new Date().getMilliseconds() + '  	 rightListData  ', this.rightListData);
    this.listdata = this.tagData.data.data.data;
  }
  // 跳转事件
  go(e) {
    if (e.isCall && e.isCall === true) {
      this.call(e.phone, e);
    } else {
      this.pageModalService.openPage(e.link);
      // const pageParams = {
      //   pageId: e.link.target,
      //   params: e.link.params,
      //   sessionId: localStorage.getItem('sessionId')
      // };
      // this.appGlobalService.goToForward(pageParams);
    }
  }
  // 拨打号码
  async call(mobile, ojb) {
    if (mobile) {
      const alert = await this.alertController.create({
        header: '铺铺旺' + " " + mobile,
        buttons: [
          {
            text: '取消',
            role: 'cancel',
            cssClass: 'secondary'
          }, {
            text: '拨打',
            handler: () => {
              this.http.changeCallNumber(mobile);
            }
          }
        ]
      });
      await alert.present();
    }

  }
}
